package cn.ubeta.b2c.bean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by denny on 16/9/6.
 */

public class OrderInfo extends ApiResp<OrderInfo> {

    // 订单24小时超时
    public final static int ORDER_TIMEOUT_MS = 24 * 60 * 60 * 1000;

    public final static int STATUS_UNPAY = 0;
    public final static int STATUS_PAID = 1;
    public final static int STATUS_TIMEOUT = 2;
    public static final long PAID_FLAG = 12345678L;

    private long orderId;
    private OrderMapInfo whatYouWant;
    /**
     * resultFlag : true
     */

    private boolean resultFlag;
    /**
     * message : 错误信息
     */

    private String message;
    /**
     * orderInfo : orderInfo from server
     */

    private String orderInfo;

    public boolean isResultFlag() {
        return resultFlag;
    }

    public void setResultFlag(boolean resultFlag) {
        this.resultFlag = resultFlag;
    }

    public void setProductImg(int productImg) {
        this.productImg = productImg;
    }

    public String getOrderInfo() {
        return orderInfo;
    }

    public void setOrderInfo(String orderInfo) {
        this.orderInfo = orderInfo;
    }

    class OrderMapInfo {
        double needMoney;
    }

    /**
     * time : 1234567890
     */

    private long time;

    // 图片的资源id
    private int productImg;
    private String goodsName;

    public static List<OrderInfo> getTestData() {
        List<OrderInfo> data = new ArrayList<>();
        for (int i = 0; i < 12; ++i) {
            OrderInfo info = new OrderInfo();
            data.add(info);
        }
        return data;
    }

    public long getOrderId() {
        return orderId;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public String getFormatedTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return dateFormat.format(new Date(time));
    }

    public int getProductImg() {
        return productImg;
    }

    public static OrderInfo parse(String str) {
        OrderInfo info = new OrderInfo();
        String[] token = str.split(",");
        int i = 0;
        info.orderId = Long.parseLong(token[i++]);
        info.time = Long.parseLong(token[i++]);
        info.setProductImg(Integer.parseInt(token[i++]));
        info.setGoodsName(token[i++]);
        info.setNeedMoney(Double.parseDouble(token[i]));
        info.setOrderInfo(token[i]);
        return info;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.valueOf(orderId));
        sb.append("," + String.valueOf(time));
        sb.append("," + String.valueOf(getProductImg()));
        sb.append("," + getGoodsName());
        sb.append("," + getNeedMoney());
        sb.append("," + getOrderInfo());
        return sb.toString();
    }

    public String getFormatTotal() {
        return String.format("%.2f", getNeedMoney());
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getNeedMoney() {
        return whatYouWant != null ? whatYouWant.needMoney : 0;
    }

    public void setNeedMoney(double needMoney) {
        if (whatYouWant == null)
            whatYouWant = new OrderMapInfo();
        whatYouWant.needMoney = needMoney;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
